package com.yxym.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(new Book("三国" ,"罗贯中",30.1));
        collection.add(new Book("水浒" ,"施耐庵",50.1));
        collection.add(new Book("三体" ,"刘慈欣",40.1));
        System.out.println("collection=" + collection);

        //1.得到对应的迭代器(快捷键itit)
        //2.使用while循环
        //3.返回迭代器
        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object obj =  iterator.next();
            System.out.println( obj);
        }
        //1.使用增强for循环，在Collection集合
        //2.增强for 底层仍是迭代器
        //3.增强for可以理解简化版的迭代器
        //4.快捷方式I
        for (Object book :collection) {
            System.out.println("book=" + book);
        }
    }
}
class Book{
    private String name ;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}